﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Xml.Linq;

namespace DBAccessLibrary.Common
{
    public class ChDescriptionXml
    {
         protected readonly string _path;
         private Dictionary<string, string> _cache = new Dictionary<string, string>();
        public ChDescriptionXml()
        {
            _path = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + "QueryConfig.xml";
            VerityXml();
        }

        private void VerityXml()
        {
            if (!File.Exists(_path))
            {
                XElement root = new XElement("Query");

                XDocument document = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    root);
                document.Save(_path);
            }
        }

        protected string GetElement(string elementName)
        {
            if (_cache.ContainsKey(elementName))
                return _cache[elementName];
            XElement element = XElement.Load(_path);
            XElement xe = element.Elements().FirstOrDefault(o => o.Name.LocalName == elementName);
            if (xe == null)
            {
                return string.Empty;
            }
            return xe.Value;
        }
        protected void SetElement(string elementName, string elementValue)
        {
            if (string.IsNullOrEmpty(elementValue))
                return;
            //如果值相同 则不需要修改
            if (_cache.ContainsKey(elementName))
            {
                if (_cache[elementName] == elementValue)
                    return;
            }
            XElement element = XElement.Load(_path);
            XElement xe = element.Elements().FirstOrDefault(o => o.Name.LocalName == elementName);
            if (xe == null)
            {
                xe = new XElement(elementName);
                element.Add(xe);
            }
            xe.Value = elementValue;
            element.Save(_path);
            if (_cache.ContainsKey(elementName))
                _cache[elementName] = elementValue;
        }


        /// <summary>
        /// 中文信息
        /// </summary>
        public string ChDescription
        {
            get
            {
                return GetElement("ChDescription");
               
            }
            set
            {
                XElement element = XElement.Load(_path);
                XElement xe = element.Elements().FirstOrDefault(o => o.Name.LocalName == "ChDescription");
                if (xe == null)
                {
                    xe = new XElement("ChDescription");
                    XComment comment = new XComment("以下填写获取中文信息的SQL语句，列名必须有TABLENAME,CHINATABLENAME,COLUMNNAME,CHINACOLUMNNAME 这4个字段");
                    xe.Add(comment);
                    element.Add(xe);
                }
                xe.Value = value;
                element.Save(_path);
            }
        }
    }
}
